package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.CodePartMapper;
import com.dk.pojo.*;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.CodePartService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CodePartServiceImpl implements CodePartService {

    @Autowired
    private CodePartMapper codePartMapper;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<CodePart> getCodePartInfo(Integer pageNum, Integer pageSize, String partCode, String partName) {
        CodePartExample example = new CodePartExample();
        if(partName != null && partName.length() > 0){
            example.createCriteria().andPartNameLike("%"+partName+"%");
        }
        if(partCode != null && partCode.length() > 0){
            example.createCriteria().andPartCodeEqualTo(partCode);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CodePart> codeParts = codePartMapper.selectByExample(example);
        PageInfo<CodePart> pb = new PageInfo<>(codeParts);
        PageResult<CodePart> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addCodePart(CodePart codePart, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            codePart.setCreateBy(user.getUsername());
            codePart.setCreateTime(new Date());
            codePartMapper.insertSelective(codePart);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateCodePart(CodePart codePart, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            codePart.setUpdateBy(user.getUsername());
            codePart.setUpdateTime(new Date());
            codePartMapper.updateByPrimaryKeySelective(codePart);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteCodePart(Long partId) {
        codePartMapper.deleteByPrimaryKey(partId);
    }

    @Override
    public List<CodePart> selectPartByRuleId(Long ruleId) {
        //根据编码生成规则的id查询出编码生成规则组成的信息
        CodePartExample example = new CodePartExample();
        example.createCriteria().andRuleIdEqualTo(ruleId);
        return codePartMapper.selectByExample(example);
    }

    @Override
    public void updateCodePartAPI(CodePart codePart) {
        codePartMapper.updateByPrimaryKeySelective(codePart);
    }
}